package com.gitee.chenyaohua.oa.service.impl;

import com.gitee.chenyaohua.oa.dao.UserRepository;
import com.gitee.chenyaohua.oa.domain.TUser;
import com.gitee.chenyaohua.oa.from.dto.ToEmail;
import com.gitee.chenyaohua.oa.service.EmailService;
import com.gitee.chenyaohua.oa.service.UserService;
import com.gitee.chenyaohua.oa.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Date;

@Service("userService")
public class UserServiceImp implements UserService {


    @Autowired
    private EmailService emailService;


    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional
    public void update(TUser user) {
        user.setUpdateDate(new Date());
        userRepository.saveAndFlush(user);
    }

    @Override
    public void getVerificationCode(String email) {
        String code = verificationCodeService.resetVerificationCode(email);
        ToEmail toEmail = new ToEmail();
        toEmail.setTos(new String[]{email});
        toEmail.setContent(MessageFormat.format("验证码：{0}，当前验证码将在2分钟后失效。",code));
        toEmail.setSubject("密码修改-获取验证码");
        emailService.send(toEmail);
    }

}
